package controller

import (
	"gin/common/lib"
	"gin/middleware"
	"gin/model/body"
	"gin/service/userService"
	"gin/util/jwt"
	"gin/util/map_builder"
	"gin/util/request"

	"github.com/gin-gonic/gin"
)

type UserController struct {
}

func UserRegister(router lib.RegisterRouterGroup, needLoginRouter lib.RegisterRouterGroup) {
	user := UserController{}
	needLoginRouter.POST("/user/getUserInfo", user.getUserInfo)
	needLoginRouter.POST("/user/updateFileds", user.updateFileds)
	needLoginRouter.POST("/user/updateAvatar", user.updateAvatar)
	needLoginRouter.POST("/user/logout", user.logout)
	needLoginRouter.POST("/user/logoff", user.logoff)
	needLoginRouter.POST("/user/bindEmail", user.bindEmail)
	needLoginRouter.POST("/user/bindMobile", user.bindMobile)
	needLoginRouter.POST("/user/updatePassword", user.updatePassword)
	needLoginRouter.POST("/user/addAttention", user.addAttention)
	needLoginRouter.POST("/user/getNum", user.getNum)
	needLoginRouter.POST("/user/attentionList", user.attentionList)
	needLoginRouter.POST("/user/fansList", user.fansList)
	needLoginRouter.POST("/user/isAttention", user.isAttention)
	needLoginRouter.POST("/user/getAttentionIds", user.getAttentionIds)
}

// @Summary 获取个人主页
// @Description 获取个人主页
// @Tags 用户相关
// @Accept json
// @Router /user/getUserInfo [post]
func (*UserController) getUserInfo(c *gin.Context) {
	var p body.ToUidParam
	request.Bind(c, &p)
	toUid := p.ToUid

	uid := jwt.GetUid(c)
	if toUid == 0 {
		uid = jwt.GetUid(c, true)
	}

	info := userService.GetUserInfo(uid, toUid)

	middleware.Success(c, info)
}

// @Summary 修改个人信息
// @Description 修改个人信息
// @Tags 用户相关
// @Accept json
// @Router /user/updateFileds [post]
func (*UserController) updateFileds(c *gin.Context) {
	uid := jwt.GetUid(c, true)
	var p body.UpdateFieldsParam
	request.Bind(c, &p)

	userService.UpdateUserInfo(uid, p)

	middleware.Success(c)
}

// @Summary 修改个人头像
// @Description 修改个人头像
// @Tags 用户相关
// @Accept json
// @Router /user/updateAvatar [post]
func (*UserController) updateAvatar(c *gin.Context) {
	uid := jwt.GetUid(c, true)
	var p body.UpdateAvatarParam
	request.Bind(c, &p)

	userService.UpateAvatar(uid, p.Avatar)
	middleware.Success(c)
}

// @Summary 用户登出
// @Description 用户登出
// @Tags 用户相关
// @Accept json
// @Router /user/logout [post]
func (*UserController) logout(c *gin.Context) {
	uid := jwt.GetUid(c, true)
	userService.DelUserToken(uid)
	middleware.Success(c)
}

// @Summary 用户注销
// @Description 用户注销
// @Tags 用户相关
// @Accept json
// @Router /user/logoff [post]
func (*UserController) logoff(c *gin.Context) {
	var p body.LogoutParam
	request.Bind(c, &p)

	uid := jwt.GetUid(c, true)
	userService.UserLogoff(p, uid)

	middleware.Success(c)
}

// @Summary 绑定邮箱
// @Description 绑定邮箱
// @Tags 用户相关
// @Accept json
// @Router /user/bindEmail [post]
func (*UserController) bindEmail(c *gin.Context) {
	var p body.EmailParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	userService.UserBindEmail(p, uid)

	middleware.Success(c)
}

// @Summary 绑定手机
// @Description 绑定手机
// @Tags 用户相关
// @Accept json
// @Router /user/bindMobile [post]
func (*UserController) bindMobile(c *gin.Context) {
	var p body.MobileParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	userService.UserBindMobile(p, uid)

	middleware.Success(c)
}

// @Summary 修改密码
// @Description 修改密码
// @Tags 用户相关
// @Accept json
// @Router /user/updatePassword [post]
func (*UserController) updatePassword(c *gin.Context) {
	// 绑定参数
	var p body.UpdatePassParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	userService.UpdatePass(p, uid)

	middleware.Success(c)
}

// @Summary 添加关注
func (*UserController) addAttention(c *gin.Context) {
	var p body.ToUidParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)

	userService.AddAttention(uid, p.ToUid)
	middleware.Success(c)
}

// @Summary 获取关注和粉丝数量
func (*UserController) getNum(c *gin.Context) {
	var p body.ToUidParam
	request.Bind(c, &p)

	if p.ToUid == 0 {
		p.ToUid = jwt.GetUid(c, true)
	}
	info := userService.GetNum(p.ToUid)
	middleware.Success(c, info)
}

// @Summary 关注列表
func (*UserController) attentionList(c *gin.Context) {
	var p body.ToUidListParam
	request.Bind(c, &p)
	if p.ToUid == 0 {
		p.ToUid = jwt.GetUid(c, true)
	}

	list, count := userService.GetAttentionList(p)
	middleware.Success(c, map_builder.BuilderMap("list", list, "count", count))
}

// @Summary 粉丝列表
func (*UserController) fansList(c *gin.Context) {
	var p body.ToUidListParam
	request.Bind(c, &p)
	if p.ToUid == 0 {
		p.ToUid = jwt.GetUid(c, true)
	}

	list, count := userService.GetFansList(p)
	middleware.Success(c, map_builder.BuilderMap("list", list, "count", count))
}

// @Summary 是否关注
func (*UserController) isAttention(c *gin.Context) {
	var p body.ToUidParam
	request.Bind(c, &p)
	uid := jwt.GetUid(c, true)
	if p.ToUid == uid {
		middleware.Success(c, map_builder.BuilderMap("isAttention", false))
		return
	}
	isAttention := userService.IsAttention(uid, p.ToUid)
	middleware.Success(c, map_builder.BuilderMap("isAttention", isAttention))
}

// @Summary 获取关注id列表
func (*UserController) getAttentionIds(c *gin.Context) {
	uid := jwt.GetUid(c, true)
	ids := userService.GetAttentionIds(uid)
	middleware.Success(c, map_builder.BuilderMap("ids", ids))
}
